﻿Imports DotNetNuke
Imports DotNetNuke.Entities.Modules
Imports System.Web.UI
Imports DotNetNuke.Entities.Profile
Imports Telerik.Web.UI
Imports System.Collections.Generic


Namespace dnnWerk.Modules.Users

    Partial Class uc_Settings
        Inherits UserModuleBase

#Region "Private Members"
        Private _reports As List(Of UserReportInfo)
#End Region

#Region "Event Handlers"

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                If Not Page.IsPostBack Then

                    If DotNetNuke.Security.Permissions.ModulePermissionController.HasModuleAccess(SecurityAccessLevel.Edit, "EDIT", Me.ModuleConfiguration) = False Then
                        Response.Redirect(NavigateURL(TabId))
                    End If

                    BindModes()
                    BindLocales()
                    BindPages()
                    BindSettings()

                    lblReportsHeader.Text = Localization.GetString("lblReports", LocalResourceFile)
                    cmdAddReport.Text = Localization.GetString("cmdAddReport", LocalResourceFile)
                    cmdDeleteReport.Text = Localization.GetString("cmdDeleteReport", LocalResourceFile)
                    cmdUpdateReport.Text = Localization.GetString("cmdUpdateReport", LocalResourceFile)
                    cmdAddReport.Text = Localization.GetString("cmdUpdateReport", LocalResourceFile)
                    cmdNewReport.Text = Localization.GetString("cmdNewReport", LocalResourceFile)
                    cmdCancelReport.Text = Localization.GetString("cmdCancelReport", LocalResourceFile)
                    cmdSaveTemplate.Text = Localization.GetString("cmdSaveTemplate", LocalResourceFile)
                    cmdRestoreDefault.Text = Localization.GetString("cmdRestoreDefault", LocalResourceFile)

                End If
            Catch exc As Exception
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

        Protected Sub cmdUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
            UpdateSettings()
            Response.Redirect(NavigateURL(), True)
        End Sub

#End Region

#Region "Private Methods"

        Private Sub BindSettings()

            If Not Settings("DecorateForm") Is Nothing Then
                Me.chkDecorate.Checked = Convert.ToBoolean(Settings("DecorateForm"))
            Else
                Me.chkDecorate.Checked = True
            End If

            If Not Settings("NotifyUser") Is Nothing Then
                Me.chkNotifyUser.Checked = Convert.ToBoolean(Settings("NotifyUser"))
            Else
                Me.chkNotifyUser.Checked = False
            End If

            If Not Settings("ControlToLoad") Is Nothing Then
                Me.drpMode.SelectedValue = Settings("ControlToLoad")
            End If

            BindPanels()
            InitializeTemplate(drpMode.SelectedValue)
            BindTemplates()

            'register control settings

            If Not Settings("PreloadList") Is Nothing Then
                Me.chkPreloadList.Checked = Convert.ToBoolean(Settings("PreloadList"))
            End If

            If Not Settings("CanRunReports") Is Nothing Then
                Me.chkRunReports.Checked = Convert.ToBoolean(Settings("CanRunReports"))
            End If

            If Not Settings("AllowExport") Is Nothing Then
                Me.chkRunExport.Checked = Convert.ToBoolean(Settings("AllowExport"))
            End If

            If Not Settings("ExportFields") Is Nothing Then
                Dim strValues As String = Convert.ToString(Settings("ExportFields"))
                If strValues.Length > 0 Then
                    txtExportFields.Text = strValues
                Else
                    txtExportFields.Text = ""
                End If
            Else
                txtExportFields.Text = ""
            End If

            If Not Settings("ShowUserDetailTabs") Is Nothing Then
                Dim strValues As String = Convert.ToString(Settings("ShowUserDetailTabs"))
                If strValues.Length > 0 Then                    
                    txtShowTabs.Text = strValues
                Else
                    txtShowTabs.Text = "Account,Password,Profile,Roles,Email,Sites"
                End If
            Else
                txtShowTabs.Text = "Account,Password,Profile,Roles,Email,Sites"
            End If

            If Not Settings("AdditionalControls") Is Nothing Then
                Me.txtAdditionalControls.Text = Convert.ToString(Settings("AdditionalControls"))
            End If

            If Not Settings("ControlSkin") Is Nothing Then
                Me.drpControlSkins.FindItemByText(Convert.ToString(Settings("ControlSkin"))).Selected = True
            End If

            If Not Settings("ShowUserName") Is Nothing Then
                Me.drpShowUsername.SelectedValue = Convert.ToString(Settings("ShowUserName"))
            End If
            If Not Settings("ShowDisplayName") Is Nothing Then
                Me.drpShowDisplayname.SelectedValue = Convert.ToString(Settings("ShowDisplayName"))
            End If

            If Not Settings("RedirectAfterVerify") Is Nothing Then
                If IsNumeric(Convert.ToString(Settings("RedirectAfterVerify"))) Then
                    Try
                        Me.drpPagesAfterVerify.SelectedValue = Convert.ToString(Settings("RedirectAfterVerify"))
                    Catch
                    End Try
                Else
                    Me.drpPages.Text = Convert.ToString(Settings("RedirectAfterVerify"))
                End If
            End If

            If Not Settings("RedirectAfterSubmit") Is Nothing Then
                If IsNumeric(Convert.ToString(Settings("RedirectAfterSubmit"))) Then
                    Try
                        Me.drpPages.SelectedValue = Convert.ToString(Settings("RedirectAfterSubmit"))
                    Catch
                    End Try
                Else
                    Me.drpPages.Text = Convert.ToString(Settings("RedirectAfterSubmit"))
                End If
            End If

            If Not Settings("UsermanagementTab") Is Nothing Then
                If IsNumeric(Convert.ToString(Settings("UsermanagementTab"))) Then
                    Try
                        Me.drpUsermanagementTab.SelectedValue = Convert.ToString(Settings("UsermanagementTab"))
                    Catch
                    End Try
                End If
            End If


            If Not Settings("NotifyAddress") Is Nothing Then
                Me.txtNotify.Text = Convert.ToString(Settings("NotifyAddress"))
            End If

            If Not Settings("AddToRoleOnSubmit") Is Nothing Then
                Me.drpAddToRole.SelectedValue = Convert.ToString(Settings("AddToRoleOnSubmit"))
            End If


            If Not Settings("PreSelectRole") Is Nothing Then
                Me.drpPreSelectRole.SelectedValue = Convert.ToString(Settings("PreSelectRole"))
            End If

            If Not Settings("AllowDelete") Is Nothing Then
                Me.chkAllowDelete.Checked = Convert.ToBoolean(Settings("AllowDelete"))
            End If

            If Not Settings("AllowHardDelete") Is Nothing Then
                Me.chkAllowHardDelete.Checked = Convert.ToBoolean(Settings("AllowHardDelete"))
            End If

            If Not Settings("AllowCreate") Is Nothing Then
                Me.chkAllowCreate.Checked = Convert.ToBoolean(Settings("AllowCreate"))
            End If

            If Not Settings("RemoveFromRoleOnSubmit") Is Nothing Then
                Me.drpRemoveFromRole.SelectedValue = Convert.ToString(Settings("RemoveFromRoleOnSubmit"))
            End If

            If Not Settings("BrowseRoles") Is Nothing Then
                Dim Roles As String = Convert.ToString(Settings("BrowseRoles"))
                For Each sRole As String In Roles.Split(Char.Parse(";"))
                    For Each item As ListItem In chkBrowseRoles.Items
                        If item.Value.ToLower = sRole.ToLower Then
                            item.Selected = True
                        End If
                    Next
                Next
            Else
                For Each item As ListItem In chkBrowseRoles.Items
                    item.Selected = True
                Next
            End If

            If Not Settings("FilterSelectByRole") Is Nothing Then
                Me.drpFilterSelectByRole.SelectedValue = Convert.ToString(Settings("FilterSelectByRole"))
            End If

        End Sub

        Private Sub BindPanels()

            Select Case Me.drpMode.SelectedValue

                Case "ManageUsers"

                    Me.rowDecorate.Visible = False
                    Me.rowFilterByRole.Visible = True
                    Me.rowNotifyAfterSubmit.Visible = False
                    Me.rowRedirectAfterSubmit.Visible = False
                    Me.rowShowDisplayname.Visible = False
                    Me.rowShowUsername.Visible = False
                    Me.rowAddToRole.Visible = False
                    Me.rowRemoveFromRole.Visible = False
                    Me.rowFilterSelectByRole.Visible = False
                    Me.rowPreloadList.Visible = False
                    Me.rowRunReports.Visible = True
                    Me.rowAdditionalControls.Visible = True
                    Me.rowShowTabs.Visible = True
                    Me.rowNotifyUser.Visible = False
                    Me.rowUsermanagementTab.Visible = False
                    Me.rowRedirectAfterVerification.Visible = False
                    Me.rowPreSelectRole.Visible = True
                    Me.rowAllowCreate.Visible = True
                    Me.rowAllowDelete.Visible = True
                    Me.rowAllowHardDelete.Visible = True
                    Me.rowExportFields.Visible = True
                    Me.rowRunExport.Visible = True

                    BindRoles()
                    BindReports()

                    If UserInfo.IsSuperUser Then
                        Me.rowReports.Visible = True
                        Me.rowRunReports.Visible = True
                    Else
                        Me.rowReports.Visible = False
                        Me.rowRunReports.Visible = (_reports.Count > 0)
                    End If

                Case "UserRegister"

                    Me.rowDecorate.Visible = True
                    Me.rowNotifyAfterSubmit.Visible = True
                    Me.rowRedirectAfterSubmit.Visible = True
                    Me.rowShowDisplayname.Visible = True
                    Me.rowShowUsername.Visible = True
                    Me.rowAddToRole.Visible = True
                    Me.rowRemoveFromRole.Visible = False
                    Me.rowFilterByRole.Visible = False
                    Me.rowFilterSelectByRole.Visible = False
                    Me.rowPreloadList.Visible = False
                    Me.rowRunReports.Visible = False
                    Me.rowAdditionalControls.Visible = False
                    Me.rowShowTabs.Visible = False
                    Me.rowReports.Visible = False
                    Me.rowNotifyUser.Visible = False
                    Me.rowUsermanagementTab.Visible = True
                    Me.rowRedirectAfterVerification.Visible = True
                    Me.rowPreSelectRole.Visible = False
                    Me.rowAllowCreate.Visible = False
                    Me.rowAllowDelete.Visible = False
                    Me.rowAllowHardDelete.Visible = False
                    Me.rowExportFields.Visible = False
                    Me.rowRunExport.Visible = False

                    BindRoles()

                Case "UserProfile"

                    Me.rowDecorate.Visible = True
                    Me.rowNotifyAfterSubmit.Visible = True
                    Me.rowRedirectAfterSubmit.Visible = True
                    Me.rowShowDisplayname.Visible = True
                    Me.rowShowUsername.Visible = False
                    Me.rowAddToRole.Visible = True
                    Me.rowRemoveFromRole.Visible = True
                    Me.rowFilterByRole.Visible = False
                    Me.rowFilterSelectByRole.Visible = False
                    Me.rowPreloadList.Visible = False
                    Me.rowRunReports.Visible = False
                    Me.rowAdditionalControls.Visible = False
                    Me.rowShowTabs.Visible = False
                    Me.rowReports.Visible = False
                    Me.rowNotifyUser.Visible = True
                    Me.rowUsermanagementTab.Visible = True
                    Me.rowRedirectAfterVerification.Visible = False
                    Me.rowPreSelectRole.Visible = False
                    Me.rowAllowCreate.Visible = False
                    Me.rowAllowDelete.Visible = False
                    Me.rowAllowHardDelete.Visible = False
                    Me.rowExportFields.Visible = False
                    Me.rowRunExport.Visible = False

                    BindRoles()

                Case "Memberlist"

                    Me.rowDecorate.Visible = False
                    Me.rowFilterByRole.Visible = True
                    Me.rowNotifyAfterSubmit.Visible = False
                    Me.rowRedirectAfterSubmit.Visible = False
                    Me.rowShowDisplayname.Visible = False
                    Me.rowShowUsername.Visible = False
                    Me.rowAddToRole.Visible = False
                    Me.rowRemoveFromRole.Visible = False
                    Me.rowFilterSelectByRole.Visible = False
                    Me.rowPreloadList.Visible = False
                    Me.rowRunReports.Visible = False
                    Me.rowAdditionalControls.Visible = False
                    Me.rowShowTabs.Visible = False
                    Me.rowReports.Visible = False
                    Me.rowNotifyUser.Visible = False
                    Me.rowRedirectAfterVerification.Visible = False
                    Me.rowPreSelectRole.Visible = False
                    Me.rowAllowCreate.Visible = False
                    Me.rowAllowDelete.Visible = False
                    Me.rowAllowHardDelete.Visible = False
                    Me.rowExportFields.Visible = False
                    Me.rowRunExport.Visible = False

                    BindRoles()

                Case "Searchform"

                    Me.rowFilterByRole.Visible = True
                    Me.rowFilterSelectByRole.Visible = True
                    Me.rowNotifyAfterSubmit.Visible = False
                    Me.rowRedirectAfterSubmit.Visible = False
                    Me.rowShowDisplayname.Visible = False
                    Me.rowShowUsername.Visible = False
                    Me.rowAddToRole.Visible = False
                    Me.rowRemoveFromRole.Visible = False
                    Me.rowPreloadList.Visible = True
                    Me.rowRunReports.Visible = True
                    Me.rowAdditionalControls.Visible = False
                    Me.rowShowTabs.Visible = False
                    Me.rowReports.Visible = False
                    Me.rowNotifyUser.Visible = False
                    Me.rowRedirectAfterVerification.Visible = False
                    Me.rowPreSelectRole.Visible = False
                    Me.rowAllowCreate.Visible = False
                    Me.rowAllowDelete.Visible = False
                    Me.rowAllowHardDelete.Visible = False
                    Me.rowExportFields.Visible = False
                    Me.rowRunExport.Visible = False

                    BindRoles()

            End Select


        End Sub

        Private Sub BindReports()


            _reports = UserReportsController.GetReports(PortalId)
            rptReports.DataSource = _reports
            rptReports.DataBind()

        End Sub

        Private Sub BindRoles()

            drpRemoveFromRole.Items.Clear()
            drpAddToRole.Items.Clear()

            Dim rc As New Security.Roles.RoleController
            Dim roles As ArrayList = rc.GetPortalRoles(PortalId)

            drpRemoveFromRole.DataSource = roles
            drpRemoveFromRole.DataTextField = "RoleName"
            drpRemoveFromRole.DataValueField = "RoleID"
            drpRemoveFromRole.DataBind()
            drpRemoveFromRole.Items.Insert(0, New ListItem("---", "-1"))

            drpAddToRole.DataSource = roles
            drpAddToRole.DataTextField = "RoleName"
            drpAddToRole.DataValueField = "RoleID"
            drpAddToRole.DataBind()
            drpAddToRole.Items.Insert(0, New ListItem("---", "-1"))

            drpPreSelectRole.DataSource = roles
            drpPreSelectRole.DataTextField = "RoleName"
            drpPreSelectRole.DataValueField = "RoleID"
            drpPreSelectRole.DataBind()
            drpPreSelectRole.Items.Insert(0, New ListItem("---", "-1"))

            chkBrowseRoles.DataSource = roles
            chkBrowseRoles.DataTextField = "RoleName"
            chkBrowseRoles.DataValueField = "RoleID"
            chkBrowseRoles.DataBind()

            chkBrowseRoles.Items.Add(New ListItem("Deleted Users", "-2"))

            drpFilterSelectByRole.DataSource = roles
            drpFilterSelectByRole.DataTextField = "RoleName"
            drpFilterSelectByRole.DataValueField = "RoleID"
            drpFilterSelectByRole.DataBind()
            drpFilterSelectByRole.Items.Insert(0, New ListItem("---", "-1"))

        End Sub

        Private Sub UpdateSettings()

            'generell settings
            Dim ctl As New ModuleController
            ctl.UpdateModuleSetting(ModuleId, "DecorateForm", chkDecorate.Checked)
            ctl.UpdateModuleSetting(ModuleId, "ControlToLoad", drpMode.SelectedValue)
            ctl.UpdateModuleSetting(ModuleId, "PreloadList", chkPreloadList.Checked)
            ctl.UpdateModuleSetting(ModuleId, "AdditionalControls", txtAdditionalControls.Text)
            ctl.UpdateModuleSetting(ModuleId, "NotifyUser", chkNotifyUser.Checked.ToString)
            ctl.UpdateModuleSetting(ModuleId, "UsermanagementTab", drpUsermanagementTab.SelectedValue)
            ctl.UpdateModuleSetting(ModuleId, "PreSelectRole", drpPreSelectRole.SelectedValue)
            ctl.UpdateModuleSetting(ModuleId, "AllowDelete", chkAllowDelete.Checked.ToString)
            ctl.UpdateModuleSetting(ModuleId, "AllowHardDelete", chkAllowHardDelete.Checked.ToString)
            ctl.UpdateModuleSetting(ModuleId, "AllowCreate", chkAllowCreate.Checked.ToString)
            ctl.UpdateModuleSetting(ModuleId, "AllowExport", chkRunExport.Checked.ToString)
            ctl.UpdateModuleSetting(ModuleId, "ExportFields", txtExportFields.Text)

            'register control settings

            ctl.UpdateModuleSetting(ModuleId, "ShowUserName", drpShowUsername.SelectedValue)
            ctl.UpdateModuleSetting(ModuleId, "ShowDisplayName", drpShowDisplayname.SelectedValue)
            ctl.UpdateModuleSetting(ModuleId, "ControlSkin", drpControlSkins.SelectedItem.Text)

            If drpPages.SelectedItem Is Nothing Then
                ctl.UpdateModuleSetting(ModuleId, "RedirectAfterSubmit", Me.drpPages.Text)
            Else
                ctl.UpdateModuleSetting(ModuleId, "RedirectAfterSubmit", Me.drpPages.SelectedValue)
            End If

            If drpPagesAfterVerify.SelectedItem Is Nothing Then
                ctl.UpdateModuleSetting(ModuleId, "RedirectAfterVerify", Me.drpPagesAfterVerify.Text)
            Else
                ctl.UpdateModuleSetting(ModuleId, "RedirectAfterVerify", Me.drpPagesAfterVerify.SelectedValue)
            End If



            ctl.UpdateModuleSetting(ModuleId, "NotifyAddress", Me.txtNotify.Text.Trim)


            ctl.UpdateModuleSetting(ModuleId, "AddToRoleOnSubmit", Me.drpAddToRole.SelectedValue)
            ctl.UpdateModuleSetting(ModuleId, "RemoveFromRoleOnSubmit", Me.drpRemoveFromRole.SelectedValue)

            Dim roles As String = ""
            For Each item As ListItem In chkBrowseRoles.Items
                If item.Selected Then
                    roles += item.Value & ";"
                End If
            Next
            ctl.UpdateModuleSetting(ModuleId, "BrowseRoles", roles)
            ctl.UpdateModuleSetting(ModuleId, "ShowUserDetailTabs", txtShowTabs.Text)

            ctl.UpdateModuleSetting(ModuleId, "CanRunReports", chkRunReports.Checked.ToString)
            ctl.UpdateModuleSetting(ModuleId, "FilterSelectByRole", Me.drpFilterSelectByRole.SelectedValue)

            Try
                DataCache.RemoveCache("dnnwerk_Userlist")
            Catch
            End Try

        End Sub

        Private Sub BindPages()

            Dim tabs As System.Collections.Generic.List(Of Entities.Tabs.TabInfo) = TabController.GetPortalTabs(PortalId, Null.NullInteger, True, True, False, False)

            drpPages.DataSource = tabs
            drpPages.DataTextField = "IndentedTabName"
            drpPages.DataValueField = "TabId"
            drpPages.DataBind()

            drpUsermanagementTab.DataSource = tabs
            drpUsermanagementTab.DataTextField = "IndentedTabName"
            drpUsermanagementTab.DataValueField = "TabId"
            drpUsermanagementTab.DataBind()

            drpPagesAfterVerify.DataSource = tabs
            drpPagesAfterVerify.DataTextField = "IndentedTabName"
            drpPagesAfterVerify.DataValueField = "TabId"
            drpPagesAfterVerify.DataBind()

        End Sub

        Private Sub BindTemplates()

            Me.drpTemplates.Items.Clear()

            Select Case drpMode.SelectedValue
                Case ModuleOperationModes(0) 'ManageUsers
                    For Each strValue As String In UserManagementTemplates
                        Me.drpTemplates.Items.Add(New RadComboBoxItem(strValue, strValue))
                    Next
                Case ModuleOperationModes(1) 'UserProfile
                    For Each strValue As String In UserProfileTemplates
                        Me.drpTemplates.Items.Add(New RadComboBoxItem(strValue, strValue))
                    Next
                Case ModuleOperationModes(2) 'UserRegister
                    For Each strValue As String In UserRegistrationTemplates
                        Me.drpTemplates.Items.Add(New RadComboBoxItem(strValue, strValue))
                    Next
                Case ModuleOperationModes(3) 'Memberlist
                    For Each strValue As String In MemberlistTemplates
                        Me.drpTemplates.Items.Add(New RadComboBoxItem(strValue, strValue))
                    Next
            End Select

        End Sub

#End Region

        Private Sub BindModes()
            For Each strValue As String In ModuleOperationModes
                Me.drpMode.Items.Add(New RadComboBoxItem(strValue, strValue))
            Next
        End Sub

        Private Sub BindLocales()

            Dim dicLocales As Dictionary(Of String, DotNetNuke.Services.Localization.Locale) = LocaleController.Instance().GetLocales(PortalId)

            For Each objLocale As DotNetNuke.Services.Localization.Locale In dicLocales.Values

                Dim item As New ListItem
                item.Text = objLocale.Text
                item.Value = objLocale.Code

                Me.rblLocales.Items.Add(item)

            Next

            Try
                rblLocales.Items(0).Selected = True
            Catch
            End Try


        End Sub

        Protected Sub cmdLoadTemplate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdLoadTemplate.Click

            rowTemplate.Visible = True
            rowTemplateLocale.Visible = True
            LoadSelectedTemplate(drpTemplates.SelectedItem.Value, drpMode.SelectedItem.Value, rblLocales.SelectedValue)

        End Sub

        Private Sub LoadSelectedTemplate(ByVal TemplateName As String, ByVal TemplateFolder As String, ByVal Locale As String)

            If Locale = "en-US" Then
                Locale = ""
            Else
                Locale = "." & Locale
            End If

            Dim path As String = Server.MapPath(Me.ModuleDirectory & "/templates/" & TemplateFolder & "/" & ModuleId.ToString & "/" & TemplateName & Locale & ".txt")
            Dim sr As System.IO.StreamReader = Nothing

            If Not System.IO.File.Exists(path) Then
                Dim srcPath As String = Server.MapPath(Me.ModuleDirectory & "/templates/" & TemplateFolder & "/" & TemplateName & ".txt")
                If System.IO.File.Exists(srcPath) Then
                    System.IO.File.Copy(srcPath, path)
                End If
            End If

            If System.IO.File.Exists(path) Then
                sr = System.IO.File.OpenText(path)
            End If

            If Not sr Is Nothing Then
                txtTemplate.Text = sr.ReadToEnd
                sr.Close()
                sr.Dispose()
            End If

        End Sub

        Private Function GetLocaleFromTemplate(ByVal templatename As String) As String
            Dim tmp As String = templatename.ToLower.Replace(".txt", "")
            Dim tmpLocale As String = tmp.Substring(tmp.LastIndexOf(".") + 1)
            If tmpLocale.Contains("-") Then
                Return tmpLocale
            End If
            Return "en-US"
        End Function

        Protected Sub drpMode_SelectedIndexChanged(ByVal o As Object, ByVal e As Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs) Handles drpMode.SelectedIndexChanged
            InitializeTemplate(drpMode.SelectedValue)
            BindTemplates()
            LoadSelectedTemplate(drpMode.SelectedValue, drpTemplates.SelectedValue, rblLocales.SelectedValue)
            BindPanels()
        End Sub


        Private Sub InitializeTemplate(ByVal strMode As String)

            Dim path As String = Server.MapPath(Me.ModuleDirectory & "/templates/" & drpMode.SelectedValue & "/" & ModuleId.ToString & "/")

            If Not System.IO.Directory.Exists(path) Then

                System.IO.Directory.CreateDirectory(path)

                If strMode.ToLower = "userprofile" Then

                    'special case, create template dynamically from profile properties
                    Dim strTemplate As String = DefaultTemplate_UserProfile()
                    Dim strTarget As String = Server.MapPath(Me.ModuleDirectory & "/templates/" & drpMode.SelectedValue & "/")
                    strTarget += "User.Profile.txt"

                    Dim sw As New System.IO.StreamWriter(strTarget, False)
                    sw.Write(strTemplate)
                    sw.Close()
                    sw.Dispose()

                ElseIf strMode.ToLower = "userregister" Then

                    'special case, create template dynamically from profile properties
                    Dim strTemplate As String = DefaultTemplate_UserRegistration()
                    Dim strTarget As String = Server.MapPath(Me.ModuleDirectory & "/templates/" & drpMode.SelectedValue & "/")
                    strTarget += "User.Register.txt"

                    Dim sw As New System.IO.StreamWriter(strTarget, False)
                    sw.Write(strTemplate)
                    sw.Close()
                    sw.Dispose()

                ElseIf strMode.ToLower = "memberlist" Then

                    Dim strHeaderTemplate As String = DefaultTemplate_UserListHeader()
                    Dim strHeaderTarget As String = Server.MapPath(Me.ModuleDirectory & "/templates/" & drpMode.SelectedValue & "/")
                    strHeaderTarget += "User.List.Header.txt"

                    Dim sw As New System.IO.StreamWriter(strHeaderTarget, False)
                    sw.Write(strHeaderTemplate)
                    sw.Close()
                    sw.Dispose()

                    Dim strItemTemplate As String = DefaultTemplate_UserListItem()
                    Dim strItemTarget As String = Server.MapPath(Me.ModuleDirectory & "/templates/" & drpMode.SelectedValue & "/")
                    strItemTarget += "User.List.Item.txt"

                    sw = New System.IO.StreamWriter(strItemTarget, False)
                    sw.Write(strItemTemplate)
                    sw.Close()
                    sw.Dispose()

                    Dim strAltTemplate As String = DefaultTemplate_UserListItem()
                    Dim strAltTarget As String = Server.MapPath(Me.ModuleDirectory & "/templates/" & drpMode.SelectedValue & "/")
                    strAltTarget += "User.List.AlternatingItem.txt"

                    sw = New System.IO.StreamWriter(strAltTarget, False)
                    sw.Write(strAltTemplate)
                    sw.Close()
                    sw.Dispose()

                    Dim strDetailTemplate As String = DefaultTemplate_UserListDetail()
                    Dim strDetailTarget As String = Server.MapPath(Me.ModuleDirectory & "/templates/" & drpMode.SelectedValue & "/")
                    strDetailTarget += "User.List.Detail.txt"

                    sw = New System.IO.StreamWriter(strDetailTarget, False)
                    sw.Write(strDetailTemplate)
                    sw.Close()
                    sw.Dispose()


                    Dim strFooterTemplate As String = DefaultTemplate_UserListFooter()
                    Dim strFooterTarget As String = Server.MapPath(Me.ModuleDirectory & "/templates/" & drpMode.SelectedValue & "/")
                    strFooterTarget += "User.List.Footer.txt"

                    sw = New System.IO.StreamWriter(strFooterTarget, False)
                    sw.Write(strFooterTemplate)
                    sw.Close()
                    sw.Dispose()

                ElseIf strMode.ToLower = "manageusers" Then

                    'special case, create template dynamically from profile properties
                    Dim strTemplate As String = DefaultTemplate_ManageProfile()
                    Dim strTarget As String = Server.MapPath(Me.ModuleDirectory & "/templates/" & drpMode.SelectedValue & "/")
                    strTarget += "User.Profile.txt"

                    Dim sw As New System.IO.StreamWriter(strTarget, False)
                    sw.Write(strTemplate)
                    sw.Close()
                    sw.Dispose()

                    'special case, create template dynamically from profile properties
                    Dim strEditTemplate As String = DefaultTemplate_EditAccount()
                    Dim strEditTarget As String = Server.MapPath(Me.ModuleDirectory & "/templates/" & drpMode.SelectedValue & "/")
                    strEditTarget += "User.Edit.txt"

                    sw = New System.IO.StreamWriter(strEditTarget, False)
                    sw.Write(strEditTemplate)
                    sw.Close()
                    sw.Dispose()

                    'special case, create template dynamically from profile properties
                    Dim strCreateTemplate As String = DefaultTemplate_CreateAccount()
                    Dim strCreateTarget As String = Server.MapPath(Me.ModuleDirectory & "/templates/" & drpMode.SelectedValue & "/")
                    strCreateTarget += "User.Create.txt"

                    sw = New System.IO.StreamWriter(strCreateTarget, False)
                    sw.Write(strCreateTemplate)
                    sw.Close()
                    sw.Dispose()

                End If

                For Each file As String In IO.Directory.GetFiles(Server.MapPath(Me.ModuleDirectory & "/templates/" & drpMode.SelectedValue & "/"))
                    If file.EndsWith(".txt") Then
                        Dim strCopyTo As String = file.Replace(drpMode.SelectedValue & "\", drpMode.SelectedValue & "\" & ModuleId.ToString & "\")
                        System.IO.File.Copy(file, strCopyTo)
                    End If
                Next

            End If

        End Sub

        Protected Sub drpTemplates_SelectedIndexChanged(ByVal o As Object, ByVal e As Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs) Handles drpTemplates.SelectedIndexChanged

            LoadSelectedTemplate(drpTemplates.SelectedItem.Value, drpMode.SelectedItem.Value, rblLocales.SelectedValue)

        End Sub

        Protected Sub rblLocales_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rblLocales.SelectedIndexChanged

            LoadSelectedTemplate(drpTemplates.SelectedItem.Value, drpMode.SelectedItem.Value, rblLocales.SelectedValue)

        End Sub

        Protected Sub cmdSaveTemplate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSaveTemplate.Click

            Dim path As String = Server.MapPath(Me.ModuleDirectory & "/templates/" & drpMode.SelectedValue & "/" & ModuleId.ToString & "/" & drpTemplates.SelectedValue & ".txt")

            If rblLocales.SelectedValue <> "en-US" Then
                path = Server.MapPath(Me.ModuleDirectory & "/templates/" & drpMode.SelectedValue & "/" & ModuleId.ToString & "/" & drpTemplates.SelectedValue & "." & rblLocales.SelectedValue & ".txt")
            End If

            Dim sw As New System.IO.StreamWriter(path, False)
            sw.Write(txtTemplate.Text)
            sw.Close()
            sw.Dispose()

        End Sub

        Protected Sub cmdAddReport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdAddReport.Click

            Dim objReport As New UserReportInfo
            objReport.FriendlyName = txtReportName.Text
            objReport.Sql = txtReportSql.Text
            objReport.PortalId = PortalId
            objReport.NeedsParameters = False
            UserReportsController.AddReport(objReport)
            cmdNewReport.Visible = True
            pnlReportForm.Visible = False
            BindReports()

        End Sub

        Protected Sub cmdEditReport_Click(ByVal sender As Object, ByVal e As System.EventArgs)

            Dim reportid As Integer = Convert.ToInt32(CType(sender, LinkButton).CommandArgument)
            Dim objReport As UserReportInfo = UserReportsController.GetReport(reportid)
            If Not objReport Is Nothing Then
                pnlReportForm.Visible = True
                cmdUpdateReport.Visible = True
                cmdDeleteReport.Visible = True                
                cmdAddReport.Visible = False
                txtReportSql.Text = objReport.Sql
                txtReportName.Text = objReport.FriendlyName
                cmdUpdateReport.CommandArgument = reportid.ToString
                cmdDeleteReport.CommandArgument = reportid.ToString
            End If

        End Sub

        Protected Sub cmdDeleteReport_Click(ByVal sender As Object, ByVal e As System.EventArgs)

            Dim reportid As Integer = Convert.ToInt32(CType(sender, LinkButton).CommandArgument)
            Dim objReport As UserReportInfo = UserReportsController.GetReport(reportid)
            If Not objReport Is Nothing Then
                UserReportsController.DeleteReport(reportid)
                pnlReportForm.Visible = False
                BindReports()
            End If

        End Sub

        Protected Sub cmdNewReport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdNewReport.Click
            pnlReportForm.Visible = True
            cmdNewReport.Visible = False
            cmdAddReport.Visible = True
            cmdUpdateReport.Visible = False
            cmdDeleteReport.Visible = False
            txtReportName.Text = ""
            txtReportSql.Text = ""
        End Sub

        Protected Sub cmdUpdateReport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdUpdateReport.Click

            Dim reportid As Integer = Convert.ToInt32(CType(sender, LinkButton).CommandArgument)
            Dim objReport As UserReportInfo = UserReportsController.GetReport(reportid)
            If Not objReport Is Nothing Then
                pnlReportForm.Visible = False
                objReport.Sql = txtReportSql.Text
                objReport.FriendlyName = txtReportName.Text
                UserReportsController.UpdateReport(objReport)
                BindReports()
            End If

        End Sub

        Protected Sub cmdDeleteReport_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdDeleteReport.Click

            Dim reportid As Integer = Convert.ToInt32(CType(sender, LinkButton).CommandArgument)
            Dim objReport As UserReportInfo = UserReportsController.GetReport(reportid)
            If Not objReport Is Nothing Then
                UserReportsController.DeleteReport(reportid)
                pnlReportForm.Visible = False
                BindReports()
            End If

        End Sub

        Protected Sub rptReports_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptReports.ItemDataBound

            If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then

                Dim cmdEdit As LinkButton = CType(e.Item.FindControl("cmdEditReport"), LinkButton)
                If Not cmdEdit Is Nothing Then
                    cmdEdit.Text = Localization.GetString("cmdEditReport", LocalResourceFile)
                End If


                Dim cmdDelete As LinkButton = CType(e.Item.FindControl("cmdDeleteReport"), LinkButton)
                If Not cmdDelete Is Nothing Then
                    cmdDelete.Text = Localization.GetString("cmdDeleteReport", LocalResourceFile)
                End If

            End If
        End Sub

        Protected Sub cmdCancelReport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdCancelReport.Click
            pnlReportForm.Visible = False
            cmdNewReport.Visible = True
        End Sub

        Protected Sub cmdRestoreDefault_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdRestoreDefault.Click

            Select Case drpMode.SelectedValue.ToLower

                Case "userprofile"
                    txtTemplate.Text = DefaultTemplate_UserProfile()
                Case "userregistration"
                    txtTemplate.Text = DefaultTemplate_UserRegistration()
                Case "manageusers"

                    Select Case drpTemplates.SelectedValue.ToLower
                        Case "user.edit"
                            txtTemplate.Text = DefaultTemplate_EditAccount()
                        Case "user.profile"
                            txtTemplate.Text = DefaultTemplate_EditProfile()
                        Case "user.create"
                            txtTemplate.Text = DefaultTemplate_CreateAccount()
                    End Select
                    Dim path As String = Server.MapPath(Me.ModuleDirectory & "/templates/" & drpMode.SelectedValue & "/" & drpTemplates.SelectedValue & ".txt")
                    Dim sr As System.IO.StreamReader = Nothing

                    If System.IO.File.Exists(path) Then
                        sr = System.IO.File.OpenText(path)
                    End If

                    If Not sr Is Nothing Then
                        txtTemplate.Text = sr.ReadToEnd
                        sr.Close()
                        sr.Dispose()
                    End If

            End Select

        End Sub

        Protected Sub cmdBulkSelect_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdBulkSelect.Click
            Select Case cmdBulkSelect.CommandName.ToLower
                Case "unselect"
                    For Each item As ListItem In chkBrowseRoles.Items
                        item.Selected = False
                    Next
                    cmdBulkSelect.CommandName = "Select"
                    cmdBulkSelect.Text = "Select All"
                Case "select"
                    For Each item As ListItem In chkBrowseRoles.Items
                        item.Selected = True
                    Next
                    cmdBulkSelect.CommandName = "Unselect"
                    cmdBulkSelect.Text = "Unselect All"
            End Select
        End Sub

    End Class

End Namespace
